/*
 * 主界面
 * 舞者无罪
 * 2014.11.05
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "lang.h"
#include "resource.h"
#include "master.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhedit.h"
#include "mhctrlstate.h"
#include "mhstatic.h"
#include "run.h"
#include "program.h"
#include "savescreen.h"
#include "flower.h"
#include "file.h"
#include "sys.h"
#include "work.h"
#include "mac.h"
#include "param.h"
#include "io.h"
#include "test.h"
#include "com.h"

ushort g_cx_screen = 800;
ushort g_cy_screen = 600;
gal_pixel g_color[3];
BITMAP g_bmp[MAX_BMP];
char g_edit_text[MH_FILE_NAME_MAX_LEN];
int g_edit_index = 0;
struct MH_KNIT *g_knit_data = 0;
HWND g_hwnd_run = -1;
KNIT_PAGE *page;

/*
 *
 */
#define MAST_BTN_WIDTH  (160)
#define MAST_BTN_HEIGHT (40)

#define RUN_CAPTION_LEFT    (30)
#define RUN_CAPTION_TOP     (60)
#define RUN_CAPTION_WIDTH   (200)
#define RUN_CAPTION_HEIGHT  (30)
#define MINUS_TO_SECOND     60

/*
*
*/
static void CreateMasterInterface(HWND hWnd)
{
    CreateWindow (MH_BTN_NAME,
        MW_RUN_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_RUN,
        RUN_CAPTION_LEFT, RUN_CAPTION_TOP, RUN_CAPTION_WIDTH, RUN_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
    	MW_PARAM_SET_CAPTION,
    	WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
    	ID_OTHER_PARAM,
    	RUN_CAPTION_LEFT, RUN_CAPTION_TOP + RUN_CAPTION_HEIGHT,
    	RUN_CAPTION_WIDTH, RUN_CAPTION_HEIGHT,
    	hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_FLW_MANAGE_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_FLW,
        RUN_CAPTION_LEFT, RUN_CAPTION_TOP + RUN_CAPTION_HEIGHT * 2,
        RUN_CAPTION_WIDTH, RUN_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_FILE_SAVE_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_FILE,
        RUN_CAPTION_LEFT, RUN_CAPTION_TOP + RUN_CAPTION_HEIGHT * 3,
        RUN_CAPTION_WIDTH, RUN_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_MAC_TEST5_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_BTN_MAC,
        RUN_CAPTION_LEFT, RUN_CAPTION_TOP + RUN_CAPTION_HEIGHT * 4,
        RUN_CAPTION_WIDTH, RUN_CAPTION_HEIGHT,
        hWnd, 0);
    CreateWindow (MH_BTN_NAME,
        MW_TURN_OFF6_CAPTION,
        WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
        ID_TURN_OFF,
        RUN_CAPTION_LEFT, RUN_CAPTION_TOP + RUN_CAPTION_HEIGHT * 5,
        RUN_CAPTION_WIDTH, RUN_CAPTION_HEIGHT,
        hWnd, 0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch(nID)
    {
    case ID_BTN_RUN:
        {
        	PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
        break;
    case ID_OTHER_PARAM:
        {
        	CreateParamWindow(hWnd);
        }
        break;
    case ID_BTN_FLW:
        {
        	CreateFileWindow(hWnd);
        }
        break;
    case ID_BTN_FILE:
        {
        	CreateIOWindow(hWnd);
        }
        break;
    case ID_BTN_MAC:
        {
        	CreateTestWindow(hWnd);
        }
        break;
    case ID_TURN_OFF:
        break;
    }
}
/*
 *
 */
static void OnMoveFocus(HWND hWnd, int up)
{
    ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
    switch(nID)
    {
    case ID_BTN_RUN:
        if(up)
        {
            //
        }
        else
        {
            nID = ID_OTHER_PARAM;
        }
        break;
    case ID_OTHER_PARAM:
        if(up)
        {
            nID = ID_BTN_RUN;
        }
        else
        {
            nID = ID_BTN_FLW;
        }
        break;
    case ID_BTN_FLW:
        if(up)
        {
            nID = ID_OTHER_PARAM;
        }
        else
        {
            nID = ID_BTN_FILE;
        }
        break;
    case ID_BTN_FILE:
        if(up)
        {
            nID = ID_BTN_FLW;
        }
        else
        {
            nID = ID_BTN_MAC;
        }
        break;
    case ID_BTN_MAC:
        if(up)
        {
            nID = ID_BTN_FILE;
        }
        else
        {
            nID = ID_TURN_OFF;
        }
        break;
    case ID_TURN_OFF:
        if(up)
        {
            nID = ID_BTN_MAC;
        }
        break;
    }
    SetFocusChild(GetDlgItem(hWnd, nID));
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, WPARAM key)
{
    if(key == SCANCODE_1 || key == SCANCODE_ESCAPE)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_RUN));
        OnBtnCmd(hWnd, ID_BTN_RUN);
    }
    else if(key == SCANCODE_2)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_OTHER_PARAM));
        CreateParamWindow(hWnd);
    }
    else if(key == SCANCODE_3)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_FLW));
        CreateFileWindow(hWnd);
    }
    else if(key == SCANCODE_4)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_FILE));
        CreateIOWindow(hWnd);
    }
    else if(key == SCANCODE_5)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_MAC));
        CreateTestWindow(hWnd);
    }
    else if(key == SCANCODE_6)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_TURN_OFF));
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        OnMoveFocus(hWnd, FALSE);
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        OnMoveFocus(hWnd, TRUE);
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
}
/*
 * init data from file
 */
static void InitDataFromFile()
{
    int ret = 0;
    SYS_LOG *list_head = GetSysLogList(g_knit_data);
    //WritePartsFile(PARTS_PARAM_FILE, g_knit_data);
    ReadMacFile(MAC_PARAM_FILE, g_knit_data);
    ReadKnitFile(KNIT_PARAM_FILE, g_knit_data);
    ReadSysLogFile(SYS_LOG_FILE, list_head);
    char file_name[MH_FILE_NAME_MAX_LEN] = {0};
    char *sc = GetKnitFileName(g_knit_data);
    int len = 0;
    if(sc)
    {
        printf("knit file: %s\n", sc);
        len = strlen(sc);
    }
    if(len > 0 && len < MH_FILE_NAME_MAX_LEN - 4 - sizeof(KNIT_FILE_DIR))
    {
        strcat(file_name, KNIT_FILE_DIR);
        strcat(file_name, sc);
        len = strlen(file_name);
        strcat(file_name, ".stp");
        ReadDumuFile(file_name, g_knit_data);
        file_name[len] = 0;
        strcat(file_name, ".spd");
        ReadSpeedFile(file_name, g_knit_data);
        file_name[len] = 0;
        strcat(file_name, ".dat");
        ReadDatFile(file_name, g_knit_data);
        SetFlowerBoundary(g_knit_data);
        file_name[len] = 0;
        strcat(file_name, ".con");
        ReadConFile(file_name, g_knit_data);
        file_name[len] = 0;
        strcat(file_name, ".gsp");
        ReadStopFile(file_name, g_knit_data);

        file_name[len] = 0;
        strcat(file_name, CONF_FILE_SUFFIX);
        ret = ReadConfigureFile(file_name, g_knit_data);
        if(ret)
        {
            printf("failed to read conf file %s: %d\n", file_name, ret);
        }

        if(GetRockCompressEnable(g_knit_data))
        {
            CompressRockCtrlPage(g_knit_data);
        }

        DivideBackPiece(g_knit_data);
        InitKnitData(g_knit_data);
    }
}
/*
 *
 */
static int MastWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case MSG_CREATE:
        {
        	CreateMasterInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += MASTER_TITLE_LEFT;
            rect.top += MASTER_TITLE_TOP;
            rect.right = rect.left + MASTER_TITLE_WIDTH - 1;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_GREEN(hdc));
            DrawText(hdc, GetWindowCaption (hWnd), -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            HWND hHosting = GetHosting(hWnd);
            if(hHosting != HWND_INVALID)
            {
                EnableWindow(hHosting, TRUE);
            }
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        return 0;
    case MSG_COMMAND:
        OnBtnCmd(hWnd, LOWORD(wParam));
        return 0;
    case MSG_DESTROY:
        {
            KillTimer(hWnd, TIMER_ID_RUN);
            ShowWindow(hWnd, SW_HIDE);
        	DestroyAllControls(hWnd);
        }
    	break;
    case MSG_TIMER:
        {
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_MAST]);
        }
    	return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateMasterWindow(HWND hHosting)
{
    HWND hWnd;
    MAINWINCREATE CreateInfo;
    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = MW_MAST_MENU_CAPTION;
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = MastWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX;
    CreateInfo.by = y + MASTER_WND_CY;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetTimer(hWnd, TIMER_ID_RUN, 800);
        EnableWindow(hHosting, FALSE);
        SetFocusChild(GetDlgItem(hWnd, ID_BTN_RUN));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
/*
 * entrance
 */
int MiniGUIMain(int argc, const char* argv[])
{
    MSG Msg;
    HWND hMainWnd;
    HWND hwd;
    time_t last_time, cur_time;

    int ret = -1;

#ifdef _LITE_VERSION
    JoinLayer(NAME_DEF_LAYER, "mh", 0, 0);
#endif

    printf("this is the entrance of my application...\n");
    LoadMemBmp();
    RegisterMhBtn();
    RegisterMhLabel();
    RegisterMhCtrlState();
    RegisterMhEdit();
    RegisterMhStatic();
    g_knit_data = CreateKnitData(1);
    if(g_knit_data)
    {
        InitDataFromFile();
    }
    else
    {
        return ret;
    }
    //
    if(!GetSysTime(g_knit_data))
    {
        SetSysTime(g_knit_data, time(NULL));
    }

    last_time = GetSysTime(g_knit_data);
    SetSaveScreenEnable(g_knit_data, TRUE);
    //turn on machine must after init all
    TurnOnMachine(g_knit_data, TRUE);
    //
    SendGlobalParam(g_knit_data);

    hMainWnd = CreateRunWindow(HWND_DESKTOP);

    if(hMainWnd == HWND_INVALID)
    {
        goto FAIL;
    }
    g_hwnd_run = hMainWnd;

    ret = 0;

    while(GetMessage(&Msg, hMainWnd))
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);

        /* 屏幕保护　*/
        if(GetSaveScreenTime(g_knit_data))
        {
            int second = GetSaveScreenTime(g_knit_data) * MINUS_TO_SECOND;
            cur_time = time(NULL);
            if(Msg.message == MSG_KEYDOWN || Msg.message == MSG_LBUTTONDOWN)
            {
                last_time = time(NULL);
            }
            uint time_null = cur_time - last_time;
            if(time_null > second  &&  GetSaveScreenEnable(g_knit_data))
            {
                hwd = GetActiveWindow();
                CreateSaveScreenWindow(hwd);
                SetSaveScreenEnable(g_knit_data, FALSE);
            }
        }
    }
    MainWindowThreadCleanup(hMainWnd);

FAIL:
    UnregisterMhBtn();
    UnregisterMhLabel();
    UnregisterMhCtrlState();
    UnregisterMhEdit();
    UnregisterMhStatic();

    UnloadMemBmp();
    DestroyKnitData(g_knit_data);
    //DestroyAllControls(0);
    *((int*)(0)) = 0;
    return 0;
}
#ifndef _LITE_VERSION
#include <minigui/dti.c>
#endif
